import Qt 4.7
import "common" as Common

Rectangle {
    id: mainView

    width: 640; height: 360
    color: "black"

    Image {
        id: background

        source: "resources/pixmaps/Background.png"

        MouseArea {
            id: closeButton

            x: 589; width: 58; height: 58

            onClicked: Qt.quit()
        }
    }

    Rectangle {
        id: mainContainer

        function switchView(viewName) {
            console.debug("QML:MoneyManager - Switching to view: " + viewName);

            addView.visible = false
            categoryView.visible = false
            cashFlowView.visible = false
            targetView.visible = false

            if (viewName == 'AddView') {

                addView.eraseFields();
                addView.visible = true;
            }
            else if (viewName == 'CategoryView') {
                categoryView.initialize();
                categoryView.visible = true;
            }
            else if (viewName == 'CashFlowView') {
                cashFlowView.initializeListView();
                cashFlowView.visible = true;
            }
            else if (viewName == 'TargetsView') {
                targetView.visible = true;
                targetView.initialize();
            }
        }
        function updateCategories() {
            daoManager.updateTotalByCategory();
            var m_model = daoManager.getAllCategories();
            addView.updateCategories(m_model);
            categoryView.updateCategories(m_model);
            cashFlowView.updateCategories(m_model);
        }
        function updateMoneyRegisters() {
            updateCategories();
            updateGoals();
            var m_model = daoManager.getAllMoneyRegister();
            cashFlowView.updateMoneyRegisters(m_model);
        }
        function updateGoals() {
            var m_model = daoManager.getAllGoals();
            targetView.updateGoals(m_model);
        }

        width: 640; height: 360
        color: "transparent"


        Common.SplashView {
            id: splashView

            anchors.fill: parent

            onLoginConfirmed: {
                splashView.visible = false
                sideMenu.visible = true
                mainContainer.updateCategories();
                mainContainer.updateMoneyRegisters();
                mainContainer.updateGoals();
                mainContainer.switchView('AddView');
            }
        }
        Common.AddView {
            id: addView;

            x: 0; y: 48
            visible: false

            onCallMessageBox: {
                messageBox.text = mesgBox;
                messageBox.nextView = nextView;
                messageBox.state = 'hideForeground';
                messageBox.visible = true;
            }
            onCallUpdateCategories: mainContainer.updateCategories();
            onCallUpdateMoneyRegisters: mainContainer.updateMoneyRegisters();
        }
        Common.CategoryView {
            id: categoryView;

            x: 0; y: 48
            visible: false
            onCallUpdateCategories: {
                mainContainer.updateCategories();
            }
        }
        Common.CashFlowView {
            id: cashFlowView;

            x: 0; y: 48
            visible: false
            onCallEditMoney: {
                console.debug("QML:MoneyManager - Trying to change view due to callEditMoney")
                mainContainer.switchView('AddView');
                addView.activeMoneyEdit();
            }
            onCallMessageBox: {
                messageBox.text = mesgBox;
                messageBox.nextView = nextView;
                messageBox.state = 'hideForeground';
                messageBox.visible = true;
            }
            onCallUpdateMoneyRegisters: mainContainer.updateMoneyRegisters();
        }
        Common.TargetsView {
            id: targetView

            x: 0; y: 48
            visible: false

            onCallMessageBox: {
                messageBox.text = mesgBox;
                messageBox.nextView = nextView;
                messageBox.state = 'hideForeground';
                messageBox.visible = true;
            }
            onCallUpdateGoals: mainContainer.updateGoals();
        }
        Common.SideMenu {
            id: sideMenu;

            x: 524; y: 48
            visible: false

            onAdd: {
                addView.mode = 'add';
                mainContainer.switchView('AddView');
            }
            onCategories: mainContainer.switchView('CategoryView');
            onCashFlow: mainContainer.switchView('CashFlowView');
            onTargets: mainContainer.switchView('TargetsView');
        }
        Common.MessageBox {
                id: messageBox

                visible: false
                state: "hide"

                boxHeight: 200
                boxWidth: 300

                onClicked: {
                    messageBox.state = 'normal';
                    if (nextView != 'Nope')
                        mainContainer.switchView(nextView);
                }
            }

    }
}
